﻿using System.Collections.Generic;
#if UNITY_EDITOR
using UnityEditor;
#endif
using UnityEngine;
using WorldMapNew;

public class PointAdorn : MonoBehaviour
{
    [Tooltip("[1,4] 代表方向 顺时针 [5,8] 代表边 顺时针 上右下左 10 代表三级装饰物 9 代表衔接道路 11 代表空置装饰物 -1无效值 0无 但是表示两出生点相连")]
    public int type = -1;
    //[Tooltip("出生点相连时间距-用于对齐的时候用")]
    //public float lineDis = 0;
    public List<GameObject> pointObjs;// 相关出生点

    public bool Enable = true;

    public int prefabPathIdx = 0;
    public string prefabPath = "";
#if UNITY_EDITOR

    public void ChangeShowLevel(int value)
    {
        if (!Enable)
        {
            gameObject.SetActive(false);
            return;
        }
        bool bShow = false;
        if (value == 1)
        {
            bShow = (type >= 1 && type <= 4) || (type == 9);
        }
        else if (value == 2)
        {
            bShow = (type >= 5 && type <= 8) || (type == 0);
        }
        else if (value == 3)
        {
            bShow = (type == 10);
        }
        else if (value == 0)
        {
            bShow = (type == 11);
        }
        else
        {
            bShow = Enable;
        }
        gameObject.SetActive(bShow);
    }

    public void BindPoint(GameObject point)
    {
        if (pointObjs == null)
        {
            pointObjs = new List<GameObject>();
        }
        if (pointObjs != null) {
            pointObjs.Clear();
        }
        pointObjs.Add(point);
    }

    public void CheckPoints(int type1 = -1)
    {
        if (type >= 0)
        {
            type = type1;
        }
        else if (pointObjs.Count > 2)
        {
            type = 10;
            Vector3 temp = Vector3.zero;
            for (int i = 0; i < pointObjs.Count; i++)
            {
                temp += pointObjs[i].transform.position;
            }
            temp = temp / pointObjs.Count;
            transform.position = temp;
        }
        else
        {
            Vector3 pos1 = pointObjs[0].transform.position;
            Vector3 pos2 = pointObjs[1].transform.position;
            if (pos1.x == pos2.x)
            {
                if (pos1.z < pos2.z)
                {
                    type = 5;
                }
                else
                {
                    type = 7;
                }
            }
            else if (pos1.z == pos2.z)
            {
                if (pos1.x < pos2.x)
                {
                    type = 6;
                }
                else
                {
                    type = 8;
                }
            }
            else
            {
                type = -1;
            }
            transform.position = (pos1 + pos2) * 0.5f;
        }
    }


    public TilePointAdornData GetPointAdornData()
    {
        TilePointAdornData data = new TilePointAdornData();
        // 特殊处理 如果是衔接车道的话 只要显示就可以 不需要判断enable
        bool bData = Enable;
        if (type == 9) bData = gameObject.activeSelf;
        if (!bData)
        {
            return null;
        }
        data.resId = 0;
        data.type = type;
        if (pointObjs != null)
        {
            for (int i = 0; i < pointObjs.Count; i++)
            {
                Vector3 v3 = pointObjs[i].transform.position;
                int idx_x = (int)(v3.x + WorldEntry.m_iTileSize * 0.5f);
                int idx_y = (int)(v3.z + WorldEntry.m_iTileSize * 0.5f);
                data.AddPointIdx(idx_x + idx_y * WorldEntry.m_iTileSize);
            }
        }
        data.position = transform.position;
        data.rotation = transform.eulerAngles;
        //data.uid = transform.GetSiblingIndex(); 会有多层结构
        return data;
    }

    public void ClearObjs()
    {
        if (pointObjs != null)
        {
            pointObjs.Clear();
        }
    }

    public void AddObjs(GameObject obj)
    {
        if (pointObjs == null)
        {
            pointObjs = new List<GameObject>();
        }
        pointObjs.Add(obj);
    }

    public void UpdatePrefab(string prefabPath)
    {
        this.prefabPath = prefabPath;
        if (transform.childCount > 0)
        {
            GameObject.DestroyImmediate(transform.GetChild(0).gameObject);
        }

        if (prefabPath == "0类型连接出生点装饰物")
        {
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.parent = transform;
            cube.transform.localPosition = Vector3.zero;
            gameObject.name = string.Format("OBJ<{0}>", prefabPath);
        }
        else
        {
            string path = "Assets/ResourcesAssets/World/prefabs/sandtable/adorn/" + prefabPath;
            GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(path);
            GameObject cube = PrefabUtility.InstantiatePrefab(obj) as GameObject;
            cube.transform.parent = transform;
            cube.transform.localPosition = Vector3.zero;
            gameObject.name = string.Format("OBJ<{0}>", path);
        }

    }
#endif
}
